﻿using BUS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAO;
using OnlineDigitalCameraWebsite.Class;

namespace OnlineDigitalCameraWebsite
{
    public partial class DangKy : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
       
        protected void btn_DangKy_Click(object sender, ImageClickEventArgs e)
        {
            string username = txt_Username.Text;
            if (KiemTra(username))
            {
                //dang ky
                UsersBUS uBUS = new UsersBUS();
                User user = new User();
                user.userAccount = username;
                user.userPass = XuLy.MaHoaMatKhau(txt_Password.Text);
                user.userName = txt_HoTen.Text;
                user.userEmail = txtEmail.Text;
                user.userPhone = txt_DienThoai.Text;
                user.userAddress = txt_DiaChi.Text;
                user.userStatus = true;

                uBUS.Insert(user);

                Session["dangnhap"] = true;
                Session["hoten"] = user.userName;
                Session["userid"] = user.userId;
                Session["loaithanhvien"] = user.roleId;
                Response.Redirect("Default.aspx");
            }
            else
            {
                // thong bao loi
                lb_Loi.Text = "UserName này đã đăng ký";
                lb_Loi.Visible = true;
            }
        }

        private bool KiemTra(string username) // kiem tra username
        {
            UsersBUS uBUS = new UsersBUS();
            User user = new User();
            user = uBUS.KiemTraUserName(username);
            if (user == null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void btn_DangKy_Click1(object sender, EventArgs e)
        {
            string username = txt_Username.Text;
            if (KiemTra(username))
            {
                //dang ky
                UsersBUS uBUS = new UsersBUS();
                User user = new User();
                user.userAccount = username;
                user.userPass = XuLy.MaHoaMatKhau(txt_Password.Text);
                user.userName = txt_HoTen.Text;
                user.userEmail = txtEmail.Text;
                user.userPhone = txt_DienThoai.Text;
                user.userAddress = txt_DiaChi.Text;
                user.userStatus = true;

                uBUS.Insert(user);

                Session["dangnhap"] = true;
                Session["hoten"] = user.userName;
                Session["userid"] = user.userId;
                Session["loaithanhvien"] = user.roleId;
                Response.Redirect("Default.aspx");
            }
            else
            {
                // thong bao loi
                lb_Loi.Text = "UserName này đã đăng ký";
                lb_Loi.Visible = true;
            }
        }
    }
}